package jdbc;
/*
 *使用JDBC连接数据库
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo1 {
    public static void main(String[] args) throws  Exception {
        //1.加载驱动
        //2.使用驱动管理器DriverManager来获取数据库连接
        //3.获取数据库连接对象
        //4.执行sql语句
        //5.如果执行DQL则得到查询结果集
        //6.遍历查询结果集

        //1
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3307/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true",
                                                            "root",
                                                            "root");
        System.out.println("与数据库建立连接");

        Statement stmt = connection.createStatement();
        String sql = "CREATE TABLE userinfo(\n" +
                "    id          INT PRIMARY KEY AUTO_INCREMENT,\n" +
                "    username    VARCHAR(30) NOT NULL,\n" +
                "    password    VARCHAR(30) NOT NULL,\n" +
                "    nickname    VARCHAR(30) NOT NULL,\n" +
                "    age        INT(3)\n" +
                ");";
        stmt.executeUpdate(sql);
        System.out.println("执行成功");
    }
}
